package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.FlightSpecification;
import dbHelper.DBHelper;
import entities.Flight;

public class FlightRepository implements Repository<Flight> {
	
	public ArrayList<Flight> FlightList;
	
	
	public FlightRepository()
	{
		this.FlightList = null;
	}

	@Override
	public void Add(Flight Flight) {
		try {
			DBHelper.addObject(Flight);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Flight Flight) {
		try {
			DBHelper.deleteObject(Flight);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(Flight Flight) {
		try {
			DBHelper.updateObject(Flight);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		Flight F= new Flight();
		try {
			this.FlightList = (ArrayList<Flight>) DBHelper.findObjects(F);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Flight> findBySpecification (FlightSpecification spec) {
		if(this.FlightList == null)
			this.FindAll();
		ArrayList<Flight> res = new ArrayList<Flight>();
		for (Flight el : FlightList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}
	public ArrayList<Flight> findBySpecification (FlightSpecification spec,ArrayList<Flight> flightList) {
		
		ArrayList<Flight> res = new ArrayList<Flight>();
		for (Flight el : flightList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}

}
